package work.person.groups;

import work.fram.KeyValueMapper;
import work.fram.SqlMaker;
import work.person.PersonGW;
import work.person.SqlHandler;
import work.person.helper.CheckHelper;
import work.person.helper.ConditionHelper;
import work.person.helper.LoggerHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Other extends AbstractTableHandler {


    //名称
    public Other(String name) {
        setExcelFiles(name);
        setName(name);
        Map<String, String> mapMap = new HashMap<>();
        mapMap.put("gender", "性别");
        setNameCodeMap(mapMap);

    }
    private SqlMaker sqlMaker;

    private KeyValueMapper keyValueMapper;

    @Override
    public void afterHandle(Map<String, String> map) {

        String name = map.get("name");
        String idcard = map.get("idcard");

        if (CheckHelper.exist(getTableName(), ConditionHelper.create("name", "=", name), ConditionHelper.create("idcard", "=", idcard))) {
            LoggerHelper.log(getTableName() + "中已经存在相同的身份证号和姓名", name, idcard);
            return;
        }

        if (sqlMaker == null) {
            sqlMaker = new SqlMaker();
            sqlMaker.setTable(getTableName());
            keyValueMapper = new KeyValueMapper();

            //放入时间
         //   keyValueMapper.putInvariable("create_time", "2019-11-27 00:00:00");

            sqlMaker.setKeyValueMapper(keyValueMapper);
        }

        keyValueMapper.reset();

        String id = UUID.randomUUID().toString();

        //放入id
        keyValueMapper.put("id", id);
        keyValueMapper.put("person_id", ((PersonGW) getGroupsWrapper()).getId());

        map.forEach((k,v)-> keyValueMapper.put(k, v));

        if (sqlMaker.make(SqlMaker.TYPE_INSERT)) {
            SqlHandler.handle(sqlMaker.getSql());
        }

    }

}
